package com.zhiyou.mall.controller;

import com.alibaba.fastjson.JSON;
import com.zhiyou.mall.model.SCatTmpl;
import com.zhiyou.mall.model.SGoodsCat;
import com.zhiyou.mall.service.IAdminGoodsCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.management.JMException;
import java.util.*;

/**
 * @author by zhouyanchao
 * Created on 2017/11/2.
 * Descr: 商品分类示例
 */
@Controller
@RequestMapping("/goodscat")
public class AdminGoodsCatController extends AdminBaseController {

	@Autowired
	IAdminGoodsCatService catService;

	/**
	 * Created by zhouyanchao on 2017/11/2
	 * Descr:所有分类浏览页面
	 * 这里只返回页面
	 *
	 */
	@RequestMapping("demo")
	public String demo(){

		System.out.println("返回ztree演示页面");

		return "goodscat/demo";
	}

	@RequestMapping("index")
	public String index(){

		System.out.println("返回商品分类维护页面");

		return "goodscat/index";
	}

	/**
	 * Created by zhouyanchao on 2017/11/2
	 * Descr: ajax获取所有商品分类
	 *
	 */
	@ResponseBody
	@RequestMapping("ajaxdata")
	public Object ajaxdata(){

		//直接的方法，先查最上层，再查下一层
		List<SGoodsCat> goodsCat = catService.queryAllGoodsCat();

		List<Map> resultList = new ArrayList<>();
		//将结果整理为页面js所需要的格式
		for(SGoodsCat cat : goodsCat){
			HashMap map = new HashMap();
			map.put("id",cat.getId());
			map.put("pid",cat.getParentId());
			map.put("name",cat.getCatName());
			resultList.add(map);
		}
		//将整合后的list返回
		return resultList;
	}

	@RequestMapping("tmpls")
	public String cattmpl(){

		System.out.println("管理商品规格参数模板信息");

		return "goodscat/cat_tmpl";
	}

	/**
	 * Created by zhouyanchao on 2017/11/2
	 * Descr: 根据商品分类查询规格参数模板
	 * 查询此分类下所有模板，然后页面以表格形式展现，此处演示不加分页
	 *
	 */
	@RequestMapping("tmpldata")
	public Object queryTmplByCat(int catid,Model model){

		//查询分类下所有参数，然后返回表格页面(部分)
		List<SCatTmpl> tmpls = catService.queryAllTmplByCat(catid);
		model.addAttribute("results",tmpls);

		return "goodscat/tmpl_data";
	}

	/**
	 * Created by zhouyanchao on 2017/11/3
	 * Descr: 添加模板
	 *
	 */
	@ResponseBody
	@RequestMapping("tmpl/add")
	public Object addTmpl(String[] parameter,int catid,String tmpldescr){

		//数据验证


		List<String> list = new ArrayList<>();
		for(int i=0;i<parameter.length;i++){
			list.add(parameter[i]);
		}

		HashMap map = new HashMap();
		map.put("root",list);

		//转换为json
		String json = JSON.toJSONString(map);

		SCatTmpl catTmpl = new SCatTmpl();
		catTmpl.setCatId(catid);
		catTmpl.setInsertTime(new Date());
		catTmpl.setUpdateTime(new Date());
		catTmpl.setTmplParam(json);
		catTmpl.setTmplDescr(tmpldescr);

		boolean b = catService.saveCatTmpl(catTmpl);

		HashMap result = new HashMap();
		if(b){
			result.put("success",true);
		}else{
			result.put("success",false);
			result.put("msg","添加分类模板失败，请重试");
		}

		return result;
	}

	/**
	 * Created by zhouyanchao on 2017/11/2
	 * Descr: 根据模板id获取模板数据，主要是用于编辑和查看
	 *
	 */
	@ResponseBody
	@RequestMapping("tmpl/{id}/info")
	public Object queryTmplById(@PathVariable int id){

		SCatTmpl catTmpl = catService.getCatTmplById(id);

		HashMap map = new HashMap();
		if(catTmpl!=null){
			map.put("success",true);
			map.put("data",catTmpl);
		}else{
			map.put("success",false);
			map.put("msg","获取模板失败");
		}

		return map;
	}



}
